package com.baomidou.springwind.controller.supply;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springwind.controller.BaseController;
import com.baomidou.springwind.entity.*;
import com.baomidou.springwind.enums.NodeTypeEnum;
import com.baomidou.springwind.pojo.DrivingRoute;
import com.baomidou.springwind.pojo.Test;
import com.baomidou.springwind.service.CityOrderService;
import com.baomidou.springwind.service.NetworkCandidateService;
import com.baomidou.springwind.service.ProjectService;
import com.baomidou.springwind.service.WeekOrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.*;

/**
 * <p>
 * 网络规划：备选点
 * </p>
 *
 * @Author huangtao
 * @Date 2017-8-21
 */
@Controller
@RequestMapping("/network/supply")
public class NetworkCandidateController extends BaseController {

    @Autowired
    private CityOrderService cityOrderService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private NetworkCandidateService networkCandidateService;

    @RequestMapping("/candidate")
    @Permission("1001")
    public String candidateList(Model model) {
        EntityWrapper<Project> wrapper = new EntityWrapper<Project>();
        wrapper.eq("operator_id",getCurrentUserId());
        wrapper.in("state", Arrays.asList("1", "2"));
        model.addAttribute("prjList", projectService.selectList(wrapper));
        return pjaxPath("/file/network_candidate_list", model);
    }

    /**
     * 物流网络规划--备选点数据--ajax获取数据
     */
    @ResponseBody
    @RequestMapping("/candidate/getData")
    @Permission("1001")
    public String getandidateData(Page<NetworkCandidate> page, String batchId) {
        page = new Page<NetworkCandidate>(page.getCurrent(), page.getSize());
        EntityWrapper<NetworkCandidate> wrapper = new EntityWrapper<NetworkCandidate>();
        if (StringUtils.isEmpty(batchId)) {

        }
        if (batchId==null){
            batchId=getProjectId();
            if(batchId.length()==0){
                batchId="-1";
            }
        }
        wrapper.eq("batch_id", getProjectId());
        wrapper.eq("operator_id", getCurrentUserId());
        wrapper.orderBy("create_time", false);
        page = networkCandidateService.selectPage(page, wrapper);
        List<NetworkCandidate> networkCandidateList = page.getRecords();
        if (networkCandidateList.size() > 0) {
            Project project = projectService.selectById(networkCandidateList.get(0).getBatchId());
            for (NetworkCandidate networkCandidate : networkCandidateList) {
                networkCandidate.setBatchName(project == null ? "--" : project.getName());
            }
        }
        return jsonPage(page);
    }
}
